(function () {
	nsbase.views.incomeUser = new UserFunc();

	function UserFunc () {
		var self = this;

		self.init = _init;

		var incomeUrls = nsbase.config.actionUrl.income,
			utility = nsbase.utility;

		function _init () {
			initMenuBar();
			initUserTabs();

			bindCreateForm();
		}

		var cssClass = {
			selected:'selected',
			error:'error'
		};
		function initMenuBar(){
			nsbase.views.menus.init(nsbase.config.menus.income);
			nsbase.views.menus.setCurrent("user");
		}
		function initUserTabs(){
			var $tabs = $("#userTabs").find('.tab-nav'),
				$tabsContent = $(".main-bd").find('.wrp');

			$tabs.click(function() {
				var $sender = $(this);
				if ($sender.hasClass(cssClass.selected)){
					return false;
				}

				$tabs.removeClass(cssClass.selected);
				$tabsContent.hide();
				$sender.addClass(cssClass.selected);
				if($sender.hasClass('list')){
					$tabsContent.filter('.list').show();
				}
				if($sender.hasClass('add')){
					$tabsContent.filter('.add').show();
				}
				if($sender.hasClass('edit')){
					$tabsContent.filter('.edit').show();
				}
			});
		}

		function bindCreateForm(){
			var $form = $("#createForm"),
				$txtUserName = $form.find(".txtUserName"),
				$txtRealName = $form.find(".txtRealName"),
				$txtUserPwd = $form.find(".txtUserPwd"),
				$btnCreate = $form.find(".btnCreate"),
				$btnReset = $form.find(".btnReset");

			$txtUserName.blur(function() {
				emptyValidate([$txtUserName]);
				var $error = $txtUserPwd.parents('.form-group').find(cssClass.error);
				if($error.length > 0){
					return false;
				}
			});

			$txtUserPwd.blur(function() {
				emptyValidate([$txtUserPwd]);
				var $error = $txtUserPwd.parents('.form-group').find(cssClass.error);
				if($error.length > 0){
					return false;
				}
			});

			$btnReset.click(function() {
				$form.find('input').val('');
				$form.find('.' + cssClass.error).removeClass(cssClass.error);
				$form.find('.form-msg-content').empty();
			});

			$btnCreate.click(function() {
				emptyValidate([$txtUserName,$txtUserPwd]);
				if($form.find("." + cssClass.error).length > 0){
					return false;
				}

				var params = {
					userName:$txtUserName.val(),
					realName:$txtRealName.val(),
					userPwd:$txtUserPwd.val()
				};

				nsbase.ajax.request(incomeUrls.createUser,params,function(result){
					
				},function (ex) {
					
				});
			});
		}

		function bindEditForm () {
			var $form = $("#editForm"),
				$txtUserName = $form.find(".txtUserName"),
				$txtUserPwd = $form.find(".txtUserPwd");

		}

		function emptyValidate ($inputs) {
			if($inputs.length > 0){
				$.each($inputs, function(index, $input) {
					var inputValue = $input.val();
					var $inputContainer = $input.parents(".form-group").first(),
						$inputLabel = $inputContainer.find('.form-label'),
						$error = $inputContainer.find('.form-msg'),
						$errorContent = $inputContainer.find('.form-msg-content');
					if(inputValue == ""){
						$error.addClass(cssClass.error);
						$errorContent.text(utility.stringFormat('{0}不能为空!',$inputLabel.text()));
					} else {
						$error.removeClass(cssClass.error);
						$errorContent.empty();
					}
				});
			}
		}
	}

})();